package com.china08.yxyapi.api.home.activity.healthcompetition.tongjipage;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huodong.healthcompetition.YxyCompetitionDistrict;
import com.china08.yxyapi.entity.mg.huodong.healthcompetition.YxyCompetitionStudent;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;

/**
 * 健康知识竞赛 分区县统计参与学生平均分
 * 
 * @author zd.Jin
 *
 *
 */
@CrossOrigin
@RestController
public class HealthCompetionAreaAvgTongjiController extends YxyApiBaseController {
	@Autowired
	private MongoTemplate mongoTemplate;

	@RequestMapping(value = "/api_yw/home/activity/healthcompetition/huodongtongji/areaavg", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public RespModel process(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "code", defaultValue = "230100") String code) {
		RespModel respModel = new RespModel();
		List<String> noList = new ArrayList<>();
		noList.add("ff8080815ba9422a015bb326acfa1599");
		noList.add("ff8080815ac67d12015b03a25cf70a5c");
		noList.add("ff8080815a032de4015a3c0fdf2d7a16");
		noList.add("ff808081598c885c01599b2857f52de7");
		noList.add("ff80808148f98a490148fcdab1760008");
		DecimalFormat df = new DecimalFormat("######0.00");
		// 计算参与学生平均分
		TypedAggregation<YxyCompetitionStudent> totalavg = Aggregation.newAggregation(YxyCompetitionStudent.class,
				Aggregation.match(Criteria.where("status").is(2).and("schoolId").nin(noList)),
				Aggregation.group("areaCodeCity").avg("score").as("avg"));
		AggregationResults<DataModel> resultZongtotal = mongoTemplate.aggregate(totalavg, DataModel.class);
		List<DataModel> dateModelLsttotal = resultZongtotal.getMappedResults();
		if (dateModelLsttotal.size() > 0) {
			respModel.avg = Double.valueOf(df.format(dateModelLsttotal.get(0).avg));
		}
		// 分区统计参与学生平均分
		TypedAggregation<YxyCompetitionDistrict> count = Aggregation
				.newAggregation(YxyCompetitionDistrict.class,
						Aggregation.match(Criteria.where("areaCodeCity").is(code)),
						Aggregation.group("areaCodeDistrict").first("areaCodeDistrict").as("code")
								.first("areaNameDistrict").as("name").first("avg").as("avg"),
				Aggregation.sort(Sort.Direction.DESC, "avg"));
		AggregationResults<District> resultZong = mongoTemplate.aggregate(count, District.class);
		List<District> dateModelLst = resultZong.getMappedResults();
		respModel.data = dateModelLst;
		return respModel;
	}

	class DataModel {
		public Double avg;
	}

	protected static class ReqModel extends YxyApiReqModel {
	}

	protected static class RespModel extends YxyApiRespModel {
		// 分区县统计集合
		public List<District> data = new ArrayList<>();
		// 总平均分
		public Double avg;
	}

	protected static class District {
		// 区县名字
		public String name;
		// 区县id
		public String code;
		// 各区县平均分
		public Double avg;
	}
}
